package cn.blunce.weather.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

/**
 * Created by Experiment on 2016/4/20 0020.
 */
public class GetWeatherData {
    public final static String TAG = "GetWeatherData";

    public static String request(String httpUrl) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(httpUrl);
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(GetWeatherData.TAG, "Download data Error", e);
        }
        return result;
    }

    public static Bitmap getWeatherPhoto(String string_url) {
        Bitmap photo = null;
        try {
//            URL url = new URL(string_url);
//            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
//            connection.setRequestMethod("GET");
//            connection.connect();
//            photo = BitmapFactory.decodeStream(connection.getInputStream());
            InputStream in = new java.net.URL(string_url).openStream();
            photo = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(GetWeatherData.TAG, "Download photo Error", e);
        }
        return photo;
    }
}
